//
//  UserArticleViewController.m
//  product_B
//
//  Created by lanou on 16/6/2.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "UserArticleViewController.h"
#import "ReadMainModel.h"
#import "ReadserialModel.h"
#import "ReadMainTableViewCell.h"
#import "ReadTableViewController.h"
@interface UserArticleViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *bigView;
@property (nonatomic,strong)UIView *smallView;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)NSMutableArray *essayArray;
@property (nonatomic,strong)NSMutableArray *serialArray;
@end

@implementation UserArticleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self requestWork];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ReadMainTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 2;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView.backgroundColor = [UIColor whiteColor];
    self.bigView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kWidth, 50)];
    self.bigView.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"ta的文章";
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(kWidth * 0.3, 10, 40, 25);
    [button1 setTitle:@"短篇" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag = 1;
    [self.bigView addSubview:button1];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(kWidth * 0.6, 10, 40, 25);
    [button2 setTitle:@"连载" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button2.tag = 2;
    [self.bigView addSubview:button2];
    self.smallView = [[UIView alloc]initWithFrame:CGRectMake(kWidth * 0.3, 47, 40, 2)];
    self.smallView.backgroundColor = [UIColor cyanColor];
    [self.bigView addSubview:self.smallView];
    self.tableView.tableHeaderView = self.bigView;
}
- (void)buttonAction:(UIButton *)sender{
    [UIView animateWithDuration:0.3 animations:^{
        self.smallView.frame = CGRectMake(sender.frame.origin.x, 47, 40, 2);
    }];
    self.index = sender.tag - 1;
    [self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.index==0) {
        return self.essayArray.count;
    }else{
        return self.serialArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (_index==0) {
        ReadMainModel *model = self.essayArray[indexPath.row];
        cell.titleLabel.text = model.hp_title;
        NSDictionary *dic = model.author[0];
        cell.authorLabel.text = dic[@"user_name"];
        cell.contentLabel.text= model.guide_word;
        [cell.button setTitle:@"短篇" forState:UIControlStateNormal];
        
    }else{
        ReadserialModel *model = _serialArray[indexPath.row];
        cell.titleLabel.text  = model.title;
        cell.authorLabel.text = model.author[@"user_name"];
        cell.contentLabel.text = model.excerpt;
        [cell.button setTitle:@"连载" forState:UIControlStateNormal];
    }
    cell.button.layer.borderColor = [UIColor blueColor].CGColor;
    cell.button.layer.cornerRadius = 5;
    cell.button.layer.borderWidth = 1;
    cell.button.alpha = 0.5;
    cell.layer.cornerRadius = 5;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)requestWork{
    [RequestManager initWithUrlString:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/works/essay/%@",self.modelID] requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.essayArray = [ReadMainModel readDetailModelWithConfigure:dic];
        [self.tableView reloadData];
    } errorBlock:^(NSError *error) {
        
    }];
    [RequestManager initWithUrlString:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/works/serial/%@",_modelID] requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.serialArray = [ReadserialModel readDetailSerialModelWithConfigure:dic];
        [self.tableView reloadData];
    } errorBlock:^(NSError *error) {
        
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadTableViewController *tableVC = [[ReadTableViewController alloc]init];
    if (_index == 0) {
        ReadMainModel *model = self.essayArray[indexPath.row];
        tableVC.modelID= model.content_id;
        
    }else{
        ReadserialModel *model = self.serialArray[indexPath.row];
        tableVC.modelID = model.myID;
    }
    tableVC.isRihgt = YES;
    [self.navigationController pushViewController:tableVC animated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
